package com.hd.shiro2204.controller;

import com.hd.shiro2204.service.UserService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;

@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired
    UserService userService;
    @ResponseBody
    @PostMapping("/login")
    public HashMap<String,Object> login(String username,String password){
        //1.使用shiro提供的 api进行用户登录操作
        //1.1_获取shiro的核心对象Subject,该对象代表当前用户
        Subject subject = SecurityUtils.getSubject();

        // 1.2准备参数UsernamePasswordToken对象
        UsernamePasswordToken usernamePasswordToken = new UsernamePasswordToken(username,password);
        /*boolean rememberMe = usernamePasswordToken.isRememberMe();
        char [] charPassword = new char[1024];
        for (int i = 0; i <password.length() ; i++) {
            charPassword[i]=password.charAt(i);
        }
        System.out.println(charPassword);
        usernamePasswordToken.setPassword(charPassword)*/;
        //1.3使用shiro的登录方法完成登录过程
        subject.login(usernamePasswordToken);
        // 2.处理登录成功后的返回数据
        HashMap<String,Object> map = new HashMap<>();
        map.put("code",1);
        map.put("msg","登录成功");
        return map;
    }
    @PostMapping("/register")
    public String register(String username,String password){
        //调用service层代码，完成用户注烟的逻辑userService.regist(username, password) ;l
        userService.register(username,password);
        //注掰成动后，重定向到Togin . h tm l
        return "redirect:/login.html";

    }
}
